package com.joinway.framework.core.stub

import groovy.util.logging.Slf4j

import com.joinway.framework.bean.domain.Domain
import com.joinway.framework.core.bean.TestBean
import com.joinway.framework.core.bean.TestBeanSub
import com.joinway.framework.core.java.JavaDomain
//import com.joinway.framework.utils.json.LocalJsonOutput

@Slf4j
//@WithDefaultValue
class MiscTest {
	public static final String MASS = "mass"
	
	String name
	
	static main(args) {
//		String method = "test10"
//		log.info "running $method"
//		
//		MiscTest."${method}"()
		
//		println test20()
		test40()
		
//		String[] l = ['a']
//		List ll = ['a']
////		test38(['1', '2'])
//		test38(l)
//		test38(ll)
//		def (a, b, c) = test11()
//		println "a=$a, b=$b"
	}

	static void test1(){
		MiscTest mt = new MiscTest();
		println mt.name
	}
	
	static void test2(){
		TestBean bean = new TestBean()
		println "name is " + bean.getName()
	}

	static void test3(){
		TestBeanSub tbs = new TestBeanSub()
		println tbs.getName()
	}
	
	static void test4(){
		println TestService.addOne(1)
	}
	
	static void test5(){
		println UUID.randomUUID().toString()
	}
	
	static void test6(){
		int i = 10000000000
//		short s = Short.valueOf(String.valueOf(i))
//		println "s=$s"
//		int i = 100
		short j = (short)i
		println "j=$j"
		
//		boolean b = true
//		int ii = (int)b
//		println ii
		
		Integer I = 100
		short k = (short)I
		println k
	}
	
	static void test7(){
//		int i = Integer.valueOf(String.valueOf(true))
//		println "i=$i"
		
		byte b = 1
		println b
		println Byte.valueOf("1")
	}
	
	static void test8(){
		Map m = [key1:'value1', key2:'value2']
		m.each { key, value ->
			println key
			println value
		}
	}
	
	static void test9(boolean flag = true, int count){
		println flag
		println "count=$count"
	}
	
	static void test10(){
		def o = new Object()
		o.metaClass."name" = "lee"
		println o.name
		
		int i = 0
		if(i){
			println "i is true"
		}
	}
	
	static def test11(){
		['a', 'b', 'c']
//		[]
//		null
	}
	
	static void test12(){
		String name = "lee"
		String s = "hello $name"
		GString gs = "hi $name"
		s = gs
		println s
	}
	
	static void test13(){
		Set s = new HashSet()
		s = ['a', 'b', 'c']
		println s
		
		List list = s as List
		println list
		
		println list.join(",")
	}
	
	static void test14(){
		List list = ['a', 'b', 'aa', 'ab']
		List list2 = list.collect {
			it.toString().startsWith("a")
		}

		println list2
	}
	
	static void test15(){
		List list = ['a', 'b', 'c', '']
		int count = list.size()
		
		count.times {
			println it
		}
		
		println list.findAll {it.length() > 0}
	}
	
	static void test16(){
		char c = ' ' 
		println "[$c]"
		
		Date d = new Date()
		println d.getClass() == Date
	}
	
	static void test17(){
		String[] arr = ['a', 'b', 'c']
		int len = arr.length - 1
		println arr[1..len]
	}
	
	static void test18(){
		TestBean bean = new TestBean()
		println "${bean.name}"
	}
	
	static void test19(){
		def o = new TestBean()
		o.metaClass.field1 = 'lee'
		o.metaClass.field2 = 'lee2'
		
//		println o.metaClass.properties.remove(1)
		TestBean.metaClass.field2 = null
//		o.metaClass.
		o.metaClass.properties.eachWithIndex { e, i ->
			println e.name
			println i
//			ThreadManagedMetaBeanProperty prop = it
//			prop.
		}
		
		println o.field1
		println o.field2
	}
	
	static boolean test20(){
		List list = ['a', 'b', 'aa', 'ab']
		list.find{it.equals('ccc')}
	}
	
	static void test21(){
		def s = "[a,b]"
//		List list = [,]
//		println list
		s.each {
			println it
		}
//		println list
	}
	
	static void test22(){
		def s = " [ a , b] "
		String[] list = s.split(",")
		println list
		
		list.each {
			println it
			it = it.trim()
		}
		
		println list
	}
	
	static void test23(){
		String s = " [ a , b, ,c ] "
		s = s.trim().substring(1, s.length() - 3).trim()
		println s.split('\\s*,\\s*')
	}
	
	static void test24(){
		String s = "[ , ]"
		test240(s)
	}
	
	static void test240(List list){
		println list
	}
	
	static void test25(){
		Domain domain = new Domain()
		domain.metaClass."name" = 'lee'
		domain.metaClass."age" = '36'
//		println domain.name
		
//		println domain.metaClass.getProperties().get(1).name
		
		domain.getProperties().each {
			println it.key
			println it.value
			println it.value.getClass()
		}
//		domain.getClass().fields{
//			println it
//		}
	}
	
	static void test26(){
		assert "1-11" =~ /\d+-\d+/
	}
	
	static void test27(){
		String s = "aa,"
		println s.substring(0, s.length() - 1)
	}
	
	static void test28(){
		String[] arr = ['a', 'b']
		println arr.iterator().join(":")
	}
	
	static void test29(){
		String s
		switch(s){
			case null:
				println "is null"
				break
			default:
				println "not null"
		}
	}
	
//	static void test30(){
////		JsonOutput.metaClass.'static'.writeDate = {Date date, CharBuf buffer ->
////			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.PRC);
////			buffer.addQuoted(formatter.format(date));
////			formatter.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
////		}
//		
//		DateBean b = new DateBean(today: new Date())
//		println LocalJsonOutput.toJson(b)
//	}
	
	static void test31(){
		StaticService.metaClass.'static'.add2 = {int a, int b ->
			a * b
		}
		println StaticService.add(10, 2)
	}
	
	static void test32(){
		List list = []
		list << 'a'
		list << 'b' << 'c'
		println list
	}
	
	static void test33(){
		Domain d = new Domain()
		assert d instanceof GroovyObject
		
		JavaDomain jd = new JavaDomain()
		assert jd instanceof GroovyObject
	}
	
	static void test34(){
		String s = " "
		if(s){
			println "s is not empty"
		}
	}
	
	static void test35(){
		String s = " and a = ? and b = ' '  "
		int i = s.trim().indexOf(" ")
		println s.substring(i + 1, s.length())
	}
	
	static void test36(){
		TestBean bean = new TestBean()
		println bean.name
		println bean.getName()
	}
	
	static void test37(){
		String s = """
		there're '$MiscTest.MASS' person
"""
		println s
	}
	
	static void test38(Object...list){
		println list
	}
	
	static void test39(){
		List list = [new TestBean(name:"lee", age:10), new TestBean()]
		list.each{name, age ->
			println name
			println age
		}
	}
	
	static void test40(){
		List list = [1, 2, 3, 4, 5]
		println list.subList(0, 3 - 1)
		println list.subList(2, 5)
	}
	
//	@WithDefaultValue
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
